"""User repository (Synchronous)."""

from __future__ import annotations

from typing import Sequence

from sqlalchemy import func
from sqlalchemy.orm import Session

from ..models.user import User
from .base import Repository
from ..schemas.user import UserCreate, UserUpdate


class UserRepository(Repository[User, UserCreate, UserUpdate]):
    def __init__(self, session: Session) -> None:
        super().__init__(User, session)

    def get_by_email(self, email: str) -> User | None:
        """Get user by email address.

        Args:
            email: User's email address

        Returns:
            User object if found, None otherwise
        """
        return self.session.query(User).filter(User.email == email).first()

    def list_users(self) -> Sequence[User]:
        """Get all users without pagination.

        Returns:
            Sequence of all User objects in the database

        Note:
            For large datasets, consider using the inherited list() method
            which supports pagination (skip, limit parameters).
        """
        return self.session.query(User).all()

    def get_by_name(self, full_name: str) -> User | None:
        """Get user by full name (case-insensitive).

        Args:
            full_name: User's full name (whitespace will be trimmed)

        Returns:
            User object if found, None otherwise

        Note:
            Comparison is case-insensitive for better user experience.
        """
        return self.session.query(User).filter(
            func.lower(User.full_name) == func.lower(full_name.strip())
        ).first()

    def get_by_id(self, user_id: int) -> User | None:
        """Get user by ID.

        Args:
            user_id: User's unique identifier

        Returns:
            User object if found, None otherwise
        """
        return self.session.query(User).filter(User.id == user_id).first()
